<?php

declare(strict_types=1);

namespace app\admin\controller\choujiang;

use think\facade\Request;
use app\common\service\ChoujiangSend as S;
use app\common\model\ChoujiangSend as M;
use app\common\util\Excel;

class Send extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    public function index()
    {
        if (Request::isAjax()) {
            $filePath = Request::post("filepath");
            $absoluteFilePath = $_SERVER['DOCUMENT_ROOT'] . $filePath; // 将相对路径转换为绝对路径

            if (file_exists($absoluteFilePath)) {
                $data = Excel::read($absoluteFilePath);
                return json(['data' => $data]);
            } else {
                return json(['error' => 'File does not exist.'], 404);
            }
        }
        return $this->fetch();
    }

    

    public function sendHandler()
    {
        $filePath = Request::post("filepath");
        $absoluteFilePath = $_SERVER['DOCUMENT_ROOT'] . $filePath; // 将相对路径转换为绝对路径

        if (file_exists($absoluteFilePath)) {
            $data = Excel::read($absoluteFilePath);

            // 假设标题正如你所述
            $columns = ["序号", "中奖者", "奖品名称", "中奖金额", "发放状态", "领奖方式", "领奖信息", "大区：", "账号："];

            // 检查所有所需的列是否存在
            $headerRow = $data[0] ?? [];
            if (array_intersect($columns, array_keys($headerRow)) === $columns) {
                $outputFile = $_SERVER['DOCUMENT_ROOT'] . '/output.txt';
                $fileHandle = fopen($outputFile, 'w');
                if ($fileHandle) {
                    foreach ($data as $index => $row) {
                        fwrite($fileHandle, "第 " . ($index + 1) . " 行:\n");
                        foreach ($columns as $column) {
                            if (!empty($row[$column])) {
                                fwrite($fileHandle, "  $column: " . $row[$column] . "\n");
                            }
                        }
                    }
                    fclose($fileHandle);
                    return json(['message' => 'Data written to file.', 'file' => $outputFile]);
                } else {
                    return json(['error' => 'Could not open output file.'], 500);
                }
            } else {
                return json(['error' => 'Excel file does not contain all required columns.'], 400);
            }
        } else {
            return json(['error' => 'File does not exist.'], 404);
        }
    }




    // 添加
    public function add()
    {
        if (Request::isAjax()) {
            return $this->getJson(S::goAdd(Request::post()));
        }
        return $this->fetch();
    }

    // 编辑
    public function edit($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(), $id));
        }
        return $this->fetch('', ['model' => M::find($id)]);
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle()
    {
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle());
        }
        return $this->fetch();
    }
}
